<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Explicit supension with suspend/3
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot109.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot104.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot111.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc238">17.6</A>&nbsp;&nbsp;Explicit supension with suspend/3</H2>
<A NAME="suspend3"></A>
While delay-clauses are an elegant, declarative way of specifying how
a program should execute, it is sometimes necessary to be more explicit
about suspension and waking conditions.
The built-in predicate
<A HREF="../bips/kernel/suspensions/suspend-3.html"><B>suspend/3</B></A><A NAME="@default968"></A>
is provided for this purpose<SUP><A NAME="text27" HREF="umsroot104.html#note27">2</A></SUP>.
It allows to explicitly create a suspended goal, specify its priority
and its exact waking conditions.<BR>
<BR>
When
<BLOCKQUOTE CLASS="quote">
<B>suspend(Goal, Prio, CondList)</B>
</BLOCKQUOTE>
is called, <I>Goal</I> will be suspended with priority <I>Prio</I>
and it will wake up
as soon as one of the conditions specified in the <I>CondList</I>
is satisfied.
This list contains specifications of the form
<BLOCKQUOTE CLASS="quote">
<B>Vars &minus;&gt; Cond</B>
</BLOCKQUOTE>
to denote that as soon as one of the variables in the term <I>Vars</I>
satisfies the condition <I>Cond</I>, the suspended goal will
be woken and then executed as soon as the program priority allows it.
<I>CondList</I> can also be a single specification.<BR>
<BR>
The condition <I>Cond</I> can be the name of a system-defined waking condition,
e.g.
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[X,Y]-&gt;inst
</PRE></BLOCKQUOTE>
means that as soon as one (or both) of the variables <B>X, Y</B>
is instantiated, the suspended goal will be woken.
<A NAME="@default969"></A>
These variables are also called the <I>suspending variables</I> of the goal.<BR>
<BR>
Cond can also be the specification of a suspension list
defined in one of currently loaded library attributes. E.g. when the
interval solver library lib(ic) is loaded, either of
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[A,B]-&gt;ic:min
[A,B]-&gt;ic:(min of ic)
</PRE></BLOCKQUOTE>
triggers the suspended goal as soon as the minimum element
of the domain of either <B>A</B> or <B>B</B> are updated
(see Constraint Library Manual, IC Library).<BR>
<BR>
Another admissible form of condition <I>Cond</I> is
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
trigger(Name)
</PRE></BLOCKQUOTE>
which suspends the goal on the global trigger condition <B>Name</B>
(see section&nbsp;<A HREF="umsroot111.html#trigger">17.7.3</A>).<BR>
<BR>
Using
<A HREF="../bips/kernel/suspensions/suspend-3.html"><B>suspend/3</B></A><A NAME="@default970"></A>,
we can rewrite our first delay-clause example from above as follows:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
report_binding(X) :-
        ( var(X) -&gt;
            suspend(report_binding(X), 0, X-&gt;inst)
        ;
            printf("Variable has been bound to %w\n", [X])
        ).
</PRE></BLOCKQUOTE>
Here, when the predicate is called with an uninstantiated argument,
we explicitly suspend a goal with the condition that it be woken as
soon as X becomes instantiated. The priority is given as 0, which indicates
the default priority (0 is not a valid priority itself).
Running this code produces the following:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- report_binding(X).
X = X
There is 1 delayed goal.
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
When X is later instantiated, it will wake up and print the message:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- report_binding(X), writeln(here), X = 99.
here
Variable has been bound to 99
X = 99
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
<HR>
<A HREF="umsroot109.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot104.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot111.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
